package com.szh.zx.javacorecorret.chap2.one;

import java.util.concurrent.CountDownLatch;

// 章节 2.1.2
// 主要想要实现的是裁判员要等待所有的运动员全部到来的效果，主要论证的就是多个线程与同步点阻塞的特点，线程必须都要到达同步点才可以继续向下运行
// todo 这里有点异步编排的意味
public class CountDownLatchTest2 {
	public static void main(String[] args) throws InterruptedException {
		CountDownLatch maxRunner = new CountDownLatch(10);
		MyThread2[] tArray=new MyThread2[Integer.parseInt(""+maxRunner.getCount())];
		for (int i = 0; i < tArray.length; i++) {
			tArray[i] = new MyThread2(maxRunner);
			tArray[i].setName("线程"+(i+1));
			tArray[i].start();

		}
		maxRunner.await();
		System.out.println("所有线程到达同步点，都回来了");
	}
}

class MyThread2 extends Thread {
	private CountDownLatch maxRunner;

	public MyThread2(CountDownLatch maxRunner) {
		this.maxRunner = maxRunner;
	}

	@Override
	public void run() {
		try {
			Thread.sleep(2000);
			maxRunner.countDown();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}